package com.nut.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.Page;
import com.nut.pojo.Brand;
import com.nut.pojo.BrandExample;
import com.nut.pojo.BrandExample.Criteria;
import com.nut.service.brand.BrandServiceI;
import com.nut.utils.uploadImgUtils;

@Controller
@RequestMapping("/brand")
public class BrandController {
	@Resource
	BrandServiceI brandService;

	@RequestMapping("/list.do")
	public String list(Brand brand, ModelMap map) {
		BrandExample example = new BrandExample();
		//显示排序
		example.setOrderByClause("sort desc");
		//分页携带的条件
		StringBuilder params = new StringBuilder();
		//查询条件
		Criteria createCriteria = example.createCriteria();

		// 实现模糊查询
		if (StringUtils.isNotBlank(brand.getName())) {
			createCriteria.andNameLike("%" + brand.getName().trim() + "%");
			params.append("&name=" + brand.getName().trim());
		}

		// 判断是否失效
		if (brand.getIsDel() != null) {
			createCriteria.andIsDelEqualTo(brand.getIsDel());
			//System.out.println(brand.getIsDel());
			params.append("&isDel=" + brand.getIsDel());
		}

		// mybatis 相关的分页类
		Page<Brand> brands = (Page<Brand>) brandService.query(brand, example);

		brand.setUrl("list.do");
		brand.setPages(brands.getPages());
		brand.setParams(params.toString());
		map.addAttribute("brands", brands);
		map.addAttribute("brand", brand);
		return "brand/list";
	}

	@RequestMapping(value = "/add.do", method = RequestMethod.GET)
	public String goAdd(HttpServletRequest request) {
		request.setAttribute("key", "value");
		request.getSession().setAttribute("key", "session");
		return "brand/add";
	}

	@RequestMapping(value = "/add.do", method = RequestMethod.POST)
	public String doAdd(Brand brand, MultipartFile logoImg) {

		//System.out.println("品牌名:"+brand.getName());
		try {
			String str = uploadImgUtils.upload(logoImg);
			brand.setLogo(str);
			brand.setCreateTime(new Date());
			brandService.add(brand);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "redirect:list.do";
	}

	@RequestMapping(value = "/del.do")
	public String doDel(String id) {
		brandService.del(Integer.valueOf(id));
		return "redirect:list.do";
	}

	@RequestMapping(value = "/edit.do", method = RequestMethod.GET)
	public String goEdit(int id, ModelMap map) {
		map.addAttribute("brand", brandService.seleteOne(id));
		return "brand/edit";
	}

	@RequestMapping(value = "/edit.do", method = RequestMethod.POST)
	public String doEdit(Brand brand, MultipartFile logoImg) {

		try {
			String str = uploadImgUtils.upload(logoImg);
			if (str.split("\\.").length > 1) {
				brand.setLogo(str);
			}
			brand.setCreateTime(new Date());
			brandService.update(brand);
		} catch (Exception e) {
			e.printStackTrace();
		}
		

		return "redirect:list.do";
	}
}
